﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Wwtfly.DBUtility
{
    public static class DBConvert
    {
        /// <summary>
        /// 将数据库中的日期字段转换为DateTime,如果dbValue为DBNull.Value,则返回DateTime.MinValue;
        /// </summary>
        /// <param name="dbValue">要转换的数据库字段值</param>
        /// <returns>日期</returns>
        public static DateTime ToDateTime(object dbValue)
        {
            if (dbValue == null || dbValue == DBNull.Value || dbValue.ToString() == string.Empty)
            {
                return DateTime.MinValue;
            }
            return Convert.ToDateTime(dbValue);
        }

        /// <summary>
        /// 将日期型转换成数据库字段值，如果time值为time.MinValue，则转换为DBNull.Value
        /// </summary>
        /// <param name="time">要转换的日期</param>
        /// <returns></returns>
        public static Object ToDBValue(DateTime time)
        {
            return time == DateTime.MinValue ? (object)DBNull.Value : time;
        }

        public static Object ToDBValue(decimal value)
        {
            return value == decimal.MinValue ? (object)DBNull.Value : value;
        }

        /// <summary>
        /// 将日期型转换成数据库字段值，如果time值为time.MinValue，则转换为DBNull.Value
        /// </summary>
        /// <param name="d">要转换的日期</param>
        /// <returns></returns>
        public static Object ToDBValue(Double d)
        {
            return d == Double.MinValue ? (object)DBNull.Value : d;
        }

        public static Object ToDBValue(string value)
        {
            return string.IsNullOrEmpty(value) ? (object)DBNull.Value : value;
        }

        public static Object ToDBValue(int d)
        {
            return d == int.MinValue ? (object)DBNull.Value : d;
        }

        public static Object ToDBValue(byte b)
        {
            return b < byte.MinValue ? (object)DBNull.Value : b;
        }

        public static Object ToDBValue(DateTime? time)
        {
            return time.HasValue ? time.Value : (object)DBNull.Value;
        }

        public static Object ToDBValue(int? d)
        {
            return d.HasValue ? d.Value : (object)DBNull.Value;
        }

        public static Object ToDBValue(byte? b)
        {
            return b.HasValue ? b.Value : (object)DBNull.Value;
        }

        public static Object ToNullableDBValue(int? d)
        {
            return d.HasValue ? d.Value : (object)DBNull.Value;
        }

        public static Object ToNullableDBValue(byte? b)
        {
            return b.HasValue ? b.Value : (object)DBNull.Value;
        }

        /// <summary>
        /// 将整型的数据库字段值转换为System.Int32，如果值为DBNull.Value,则转换为 -1
        /// </summary>
        /// <param name="dbValue">整型的数据库值</param>
        /// <returns></returns>
        public static int ToInt32(object dbValue)
        {
            if (dbValue == null || dbValue == DBNull.Value || dbValue.ToString() == string.Empty)
            {
                return int.MinValue;
            }
            return Convert.ToInt32(dbValue);
        }

        /// <summary>
        ///  将整型的数据库字段值转换为System.Int64，如果值为DBNull.Value,则转换为 -1
        /// </summary>
        /// <param name="dbValue">整型的数据库值</param>
        /// <returns></returns>
        public static Int64 ToInt64(object dbValue)
        {
            return dbValue == null || dbValue == DBNull.Value ? Int64.MinValue : Convert.ToInt64(dbValue);
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的日期值，如果值为空，则返回DateTime.MinValue
        /// </summary>
        /// <param name="dataReader">要从中读取数据的dataReader</param>
        /// <param name="i">dataReader中的列索引</param>
        /// <returns></returns>
        public static DateTime GetDateTime(IDataReader dataReader, int i)
        {
            return dataReader.IsDBNull(i) ? DateTime.MinValue : dataReader.GetDateTime(i);
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回DateTime.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static DateTime GetDateTime(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? DateTime.MinValue : (DateTime)dataReader[field];
        }

        public static DateTime? GetNullableDateTime(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? null : (DateTime?)dataReader[field];
        }

        public static byte? GetNullableByte(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? null : (byte?)dataReader[field];
        }

        public static byte GetByte(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? (byte)0 : (byte)dataReader[field];
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的值,如果值为空，则返回Int.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="i">dataReader中的列索引</param>
        /// <returns></returns>
        public static int GetInt32(IDataReader dataReader, int i)
        {
            return dataReader.IsDBNull(i) ? int.MinValue : dataReader.GetInt32(i);
        }

        public static int? GetNullableInt32(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? null : (int?)dataReader[field];
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的值,如果值为空，则返回short.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="i">dataReader中的列索引</param>
        /// <returns></returns>
        public static short GetInt16(IDataReader dataReader, int i)
        {
            return dataReader.IsDBNull(i) ? short.MinValue : dataReader.GetInt16(i);
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回short.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static short GetInt16(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? short.MinValue : Convert.ToInt16(dataReader[field]);
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的值,如果值为空，则返回Int.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="i">dataReader中的列索引</param>
        /// <returns></returns>
        public static double GetDouble(IDataReader dataReader, int i)
        {
            return dataReader.IsDBNull(i) ? double.MinValue : dataReader.GetDouble(i);
        }

        public static decimal GetDecimal(IDataReader dataReader, int i)
        {
            return dataReader.IsDBNull(i) ? decimal.MinValue : dataReader.GetDecimal(i);
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回空串
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static decimal GetDecimal(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? decimal.MinValue : decimal.Parse(dataReader[field].ToString());
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回UInt16.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static UInt16 GetUInt16(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? UInt16.MinValue : Convert.ToUInt16(dataReader[field]);
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回Int.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static int GetInt32(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? int.MinValue : (int)dataReader[field];
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的值,如果值为空，则返回Int.MinValue
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static double GetDouble(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? double.MinValue : Convert.ToDouble(dataReader[field]);
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的值,如果值为空，则返回空串
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="i">dataReader中的列索引</param>
        /// <returns></returns>
        public static string GetString(IDataReader dataReader, int i)
        {
            return dataReader.IsDBNull(i) ? "" : dataReader.GetString(i);
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回空串
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static string GetString(IDataReader dataReader, string field)
        {
            return dataReader[field] == DBNull.Value ? "" : dataReader[field].ToString();
        }

        /// <summary>
        /// 直接从dataReader中读取字段名为field的值，如果值为空，则返回false
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="field">字段名</param>
        /// <returns></returns>
        public static bool GetBoolean(IDataReader dataReader, string field)
        {
            return dataReader[field] != DBNull.Value && (bool)dataReader[field];
        }

        /// <summary>
        /// 直接从dataReader中读取第i列的值,如果值为空，则返回false
        /// </summary>
        /// <param name="dataReader">要读取数据的dataReader</param>
        /// <param name="i">dataReader中的列索引</param>
        /// <returns></returns>
        public static bool GetBoolean(IDataReader dataReader, int i)
        {
            return !dataReader.IsDBNull(i) && dataReader.GetBoolean(i);
        }

        /// <summary>
        /// 格式化字符串，防注入式攻击
        /// </summary>
        /// <param name="str">需要格式化的字符串</param>
        /// <returns></returns>
        public static string FormatString(string str)
        {
            return str.Replace("'", "''").Replace(";", "").Replace("0x", "0 x"); //.Replace("-", "") 暂时注释
        }
    }
}
